

#include <stdio.h>
#include "uart.h"
#include "device.h"
#include "nvic.h"



static  sj_int8  uart_device_open(struct file *file_node)
{
    return 0;
}

static  sj_int8  uart_device_write(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    struct uart_device_data *self_data = (struct uart_device_data *)(file_node->private_data);
    struct peripheral_usart *usart =&self_data->param;

    UART_HandleTypeDef *handler =&(nvic_get_usart_handler(usart->id)->usart_hander);

    HAL_UART_Transmit(handler,data,len,1000);
    return 0;
}

static  sj_int8  uart_device_read(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    struct uart_device_data *self_data = (struct uart_device_data *)(file_node->private_data);



    return 0;
}

static  sj_int8  uart_device_close(struct file *file_node)
{
    return 0;
}
static  sj_int8  uart_device_poll(struct file *file_node,sj_int32 timeout)
{
    sj_int8 ret =0;

    return ret;
}


static sj_int8 uart_device_probe(struct device *dev)
{
    sj_int8 ret =0;
    sj_int8 i=0;
    struct uart_device_data *self_data =(struct uart_device_data *)dev->private_data;
    if(!self_data)
    {
        ret =-1;
        goto out;
    }

    struct rcc_apb_periph *uart_apb= self_data->uart_apb;

    struct uart_gpio *uart_pin =self_data->gpio;

    while(uart_apb->apb!=0 &&uart_apb->apb_offset!=0)
    {
        sj_gpio_clock_config(uart_apb);

        uart_apb++;
    }

    sj_gpio_init(&uart_pin->rx);

    sj_gpio_init(&uart_pin->tx);

    peripheral_usart_init(&self_data->param);

    if(self_data->nvic.irq_num>0)
    {
        peripheral_nvic_init(&self_data->nvic);
    }

out:
    return ret;
}

static sj_int8 uart_device_ioctl(struct file *file_node,sj_int8 cmd ,sj_uint8 *data)
{
    sj_int8 ret =0;

    switch(cmd)
    {

        default:
            break;
    }

    return ret;
}

static struct file_operations uart_opt =
{
    uart_device_open,
    uart_device_close,
    NULL,
    uart_device_read,
    uart_device_write,
    NULL,
    uart_device_poll,
    uart_device_ioctl,
};

static struct device_id_table uart_id_table[]=
{
    {"usart"},
    {NULL}
};

static struct device_driver usart_driver={
    "usart",
    uart_id_table,
    &uart_opt,
    uart_device_probe,
    NULL,
    NULL,
    NULL,
    NULL,
};


static DRIVER_MODULE_INIT(usart_driver)

